{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import time\n",
    "import scipy\n",
    "import json\n",
    "import Additive_mixing_layers_extraction\n",
    "from scipy.spatial import ConvexHull, Delaunay\n",
    "import scipy.sparse\n",
    "import PIL.Image as Image\n",
    "import RGBXY_method\n",
    "from numpy import *\n",
    "Additive_mixing_layers_extraction.DEMO=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_weights(img, palette_rgb, mixing_weights, output_prefix):\n",
    "    mixing_weights=mixing_weights.reshape((img.shape[0],img.shape[1],-1)).clip(0,1)\n",
    "    temp=(mixing_weights.reshape((img.shape[0],img.shape[1],-1,1))*palette_rgb.reshape((1,1,-1,3))).sum(axis=2)\n",
    "    img_diff=temp*255-img*255\n",
    "    diff=np.square(img_diff.reshape((-1,3))).sum(axis=-1)\n",
    "    print 'max diff: ', np.sqrt(diff).max()\n",
    "    print 'median diff', np.median(np.sqrt(diff))\n",
    "    rmse=np.sqrt(diff.sum()/diff.shape[0])\n",
    "    print 'RMSE: ', np.sqrt(diff.sum()/diff.shape[0])\n",
    "\n",
    "    import json\n",
    "    mixing_weights_filename=output_prefix+\"-palette_size-\"+str(len(palette_rgb))+\"-mixing_weights.js\"\n",
    "    with open(mixing_weights_filename,'wb') as myfile:\n",
    "        json.dump({'weights': mixing_weights.tolist()}, myfile)\n",
    "\n",
    "    for i in range(mixing_weights.shape[-1]):\n",
    "        mixing_weights_map_filename=output_prefix+\"-palette_size-\"+str(len(palette_rgb))+\"-mixing_weights-%02d.png\" % i\n",
    "        Image.fromarray((mixing_weights[:,:,i]*255).round().clip(0,255).astype(np.uint8)).save(mixing_weights_map_filename)\n",
    "    return rmse\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_dir=\"./test/\"\n",
    "\n",
    "import glob\n",
    "filepaths=glob.glob(base_dir+\"*.png\")\n",
    "print filepaths\n",
    "\n",
    "\n",
    "\n",
    "for filepath in filepaths:\n",
    "    print \"#####################\"\n",
    "    print filepath\n",
    "    img=np.asfarray(Image.open(filepath).convert('RGB'))/255.0\n",
    "    arr=img.copy()\n",
    "    X,Y=np.mgrid[0:img.shape[0], 0:img.shape[1]]\n",
    "    XY=np.dstack((X*1.0/img.shape[0],Y*1.0/img.shape[1]))\n",
    "    data=np.dstack((img, XY))\n",
    "    print len(data.reshape((-1,5)))\n",
    "    \n",
    "    start=time.time()\n",
    "    palette_rgb=Additive_mixing_layers_extraction.Hull_Simplification_determined_version(img, filepath[:-4]+\"-convexhull_vertices\")\n",
    "    end=time.time()    \n",
    "    M=len(palette_rgb)\n",
    "    print \"palette size: \", M\n",
    "    print \"palette extraction time: \", end-start\n",
    "    \n",
    "    palette_img=get_bigger_palette_to_show(palette_rgb)\n",
    "    Image.fromarray((palette_img*255).round().astype(np.uint8)).save(filepath[:-4]+\"-convexhull_vertices.png\")\n",
    "\n",
    "\n",
    "    ######### for RGBXY RGB black star triangulation.\n",
    "    start=time.time()\n",
    "    data_hull=ConvexHull(data.reshape((-1,5)))\n",
    "    start2=time.time()\n",
    "    print \"convexhull on 5D time: \", start2-start\n",
    "    mixing_weights_1=Additive_mixing_layers_extraction.Get_ASAP_weights_using_Tan_2016_triangulation_and_then_barycentric_coordinates(img.reshape((-1,3))[data_hull.vertices].reshape((-1,1,3)), palette_rgb, None, order=0)\n",
    "    mixing_weights_2=RGBXY_method.recover_ASAP_weights_using_scipy_delaunay(data_hull.points[data_hull.vertices], data_hull.points, option=3)\n",
    "\n",
    "    mixing_weights=mixing_weights_2.dot(mixing_weights_1.reshape((-1,M)))\n",
    "    \n",
    "    end=time.time()\n",
    "    print \"total time: \", end-start\n",
    "    \n",
    "    mixing_weights=mixing_weights.reshape((img.shape[0],img.shape[1],-1)).clip(0,1)\n",
    "    \n",
    "    output_prefix=filepath[:-4]+'-RGBXY_RGB_black_star_ASAP'\n",
    "    RMSE=save_weights(arr, palette_rgb, mixing_weights, output_prefix)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
